package com.gxa.xiaoucomeshome.controller;

import com.gxa.xiaoucomeshome.pojo.Address;
import com.gxa.xiaoucomeshome.service.AddressService;
import com.gxa.xiaoucomeshome.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@Api(value = "AddressController",tags="用户模块address相关的api接口")
@RestController
@RequestMapping("/address")
@CrossOrigin
public class AddressController {

    @Autowired
    AddressService addressService;

    /**
     * 添加收获地址的接口
     * @param address
     * @return
     */
    @ApiOperation(value = "添加地址的方法",produces = "query")
    @ApiImplicitParams({
                   @ApiImplicitParam (name = "u_id",value = "用户的id",paramType = "int"),
                    @ApiImplicitParam(name = "name",value = "收货人姓名",paramType = "String"),
                    @ApiImplicitParam(name = "phone",value = "手机号",paramType = "String"),
                    @ApiImplicitParam(name = "sparePhone",value = "备用手机号",paramType = "String"),
                    @ApiImplicitParam(name = "address",value = "收货地址",paramType = "String"),
                    @ApiImplicitParam(name = "details",value = "详细地址",paramType = "String")
    })
    @PostMapping("/addAddress")
    public Map addAddress(Integer u_id,String name,String phone,String sparePhone,String address,String details){

        addressService.insert(u_id,name,phone,sparePhone,address,details);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    /**
     * 查询地址列表
     * @param u_id
     * @return
     */
    @ApiOperation(value = "查询地址列表",produces = "query")
    @ApiImplicitParam( name = "u_id",value = "用户编号",paramType = "int")
    @PostMapping("/selectAll")
    public Map selectAll(Integer u_id){

        List<Address> addresses = addressService.selectAll(u_id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(addresses);
        return jsonResult.getMap();
    }


    @ApiOperation(value = "地址的修改方法",produces = "query")
    @ApiImplicitParams({@ApiImplicitParam (name = "id",value = "地址的id",paramType = "int"),
            @ApiImplicitParam(name = "name",value = "收货人姓名",paramType = "String"),
            @ApiImplicitParam(name = "phone",value = "手机号",paramType = "String"),
            @ApiImplicitParam(name = "sparePhone",value = "备用手机号",paramType = "String"),
            @ApiImplicitParam(name = "address",value = "收货地址",paramType = "String"),
            @ApiImplicitParam(name = "details",value = "详细地址",paramType = "String")})
    @PostMapping("/update")
    public Map update(Integer id,String name,String phone,String sparePhone,String address,String details){
        Address address2 = addressService.selectById(id);
        address2.setName(name);
        address2.setPhone(phone);
        address2.setSparePhone(sparePhone);
        address2.setAddress(address);
        address2.setDetails(details);
        addressService.update(address2);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }
    /**
     * 根据id查询地址
     * @param id
     */
    @ApiOperation(value = "根据id查询地址",produces = "query")
    @ApiImplicitParam( name = "id",value = "地址编号",paramType = "int")
    @PostMapping("/selectById")
    public Map selectById(Integer id){
        Address address = addressService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(address);
        return jsonResult.getMap();
    }
}
